package com.zhiche.lisa.bms.controller.price;

import com.baomidou.mybatisplus.plugins.Page;
import com.lisa.syslog.model.publics.PublicLog;
import com.zhiche.lisa.bms.controller.utils.ToolLogUtils;
import com.zhiche.lisa.bms.dao.model.price.PriceIncoming;
import com.zhiche.lisa.bms.service.price.PriceIncomingService;
import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping(value = "/priceIncoming")
@Api(value = "/priceIncoming", description = "收入价格接口", tags = {"收入价格接口"})
public class PriceIncomingController {
    private static final Logger LOGGER = LoggerFactory.getLogger(PriceIncomingController.class);

    @Autowired
    private PriceIncomingService priceIncomingService;

    @Autowired
    private ToolLogUtils toolLogUtils;

    @PostMapping(value = "/queryPricePurchasePage")
    @ApiOperation(value = "查询收入价格列表（分页）", notes = "查询收入价格列表（分页）", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PriceIncoming.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Page<PriceIncoming>> queryPricePurchasePage(@RequestBody Page<PriceIncoming> page) {
        RestfulResponse<Page<PriceIncoming>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            Page<PriceIncoming> pageQueryDto = priceIncomingService.queryPriceIncomingPage(page);
            result.setData(pageQueryDto);
        } catch (BaseException e) {
            LOGGER.error("/PriceController.queryPricePurchasePage ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/PriceController.queryPricePurchasePage ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @PostMapping(value = "/insertPriceIncoming")
    @ApiOperation(value = "新增收入价格列表", notes = "新增收入价格列表", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PriceIncoming.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<String> insertPriceIncoming(@RequestBody PriceIncoming obj) {

        LOGGER.info("PriceController.insertPriceIncoming（新增收入价格列表） params : {} ", obj);
        RestfulResponse<String> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        PublicLog publicLog = new PublicLog();
        try {
            publicLog = toolLogUtils.addToolSystemLog("lisa-tms", "/priceIncoming/insertPriceIncoming",
                    obj.getItemSourceKey(), obj, 0, null);
            priceIncomingService.insertPriceIncoming(obj);
            toolLogUtils.addSuccessSystemLog(publicLog);
        } catch (BaseException e) {
            LOGGER.error("PriceController.insertPriceIncoming error: {}", e);
            toolLogUtils.addErrorSystmeLog(publicLog, e.getMessage());
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("PriceController.insertPriceIncoming error: {} ", e);
            toolLogUtils.addErrorSystmeLog(publicLog, e.getMessage());
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        LOGGER.info("PriceController.insertPriceIncoming（新增收入价格列表） end");
        return result;
    }

    @PostMapping(value = "/updateIncomingPriceByTaskCode")
    @ApiOperation(value = "修改执行价格", notes = "根据（运输任务编号和运单货物明细ID）修改执行价格字段", httpMethod = "PUT", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PriceIncoming.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<String> updateIncomingPriceByTaskCode(@RequestParam String taskCode, @RequestParam Long orderItemId, @RequestParam BigDecimal incomingPrice, @RequestHeader("Authorization") String token) {
        LOGGER.info("PriceController.updateIncomingPriceByTaskCode（修改收入价格列表） taskCode : {}，orderItemId: {}, incomingPrice : {} ", taskCode, orderItemId, incomingPrice);
        RestfulResponse<String> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            priceIncomingService.updateIncomingPriceByTaskCode(taskCode, orderItemId, incomingPrice, new BigDecimal(1), token);
        } catch (BaseException e) {
            LOGGER.error("PriceController.updateIncomingPriceByTaskCode ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("PriceController.updateIncomingPriceByTaskCode ERROR {}", e);
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @GetMapping(value = "/queryCustomerlist")
    @ApiOperation(value = "查询客户列表", notes = "查询收入价格客户列表", httpMethod = "GET")
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PriceIncoming.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<PriceIncoming>> queryCustomerlist(@RequestParam String customerName) {
        RestfulResponse<List<PriceIncoming>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            List<PriceIncoming> list = priceIncomingService.queryCustomerlist(customerName);
            result.setData(list);
        } catch (BaseException e) {
            LOGGER.error("/PriceController.queryCustomerlist ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/PriceController.queryCustomerlist ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @PostMapping(value = "/deletePriceIncoming")
    @ApiOperation(value = "删除收入价格列表", notes = "根据订单编号，删除收入价格列表", httpMethod = "DELETE", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PriceIncoming.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<String> deletePriceIncoming(@RequestBody PriceIncoming obj) {

        LOGGER.info("PriceController.deletePriceIncoming（删除收入价格列表） params : {} ", obj);
        RestfulResponse<String> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        PublicLog publicLog = new PublicLog();
        try {
            publicLog = toolLogUtils.addToolSystemLog("lisa-tms", "/priceIncoming/deletePriceIncoming",
                    obj.getItemSourceKey(), obj, 0, null);
            priceIncomingService.deletePriceIncoming(obj);
        } catch (BaseException e) {
            LOGGER.error("PriceController.insertPriceIncoming ERROR {}", e);
            toolLogUtils.addErrorSystmeLog(publicLog, e.getMessage());
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("PriceController.insertPriceIncoming ERROR {}", e);
            toolLogUtils.addErrorSystmeLog(publicLog, e.getMessage());
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        LOGGER.info("PriceController.deletePriceIncoming（删除收入价格列表） end");
        return result;
    }

    @PostMapping(value = "/updatePriceIncoming")
    @ApiOperation(value = "修改收入价格列表", notes = "根据订单编号，修改收入价格列表", httpMethod = "PUT", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PriceIncoming.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<String> updatePriceIncoming(@RequestBody PriceIncoming obj) {

        LOGGER.info("PriceController.updatePriceIncoming（修改收入价格列表） params : {} ", obj);
        RestfulResponse<String> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        PublicLog publicLog = new PublicLog();
        try {
            publicLog = toolLogUtils.addToolSystemLog("lisa-tms", "/priceIncoming/updatePriceIncoming",
                    obj.getItemSourceKey(), obj, 0, null);
            priceIncomingService.updatePriceIncoming(obj);
            toolLogUtils.addSuccessSystemLog(publicLog);
        } catch (BaseException e) {
            LOGGER.error("PriceController.insertPriceIncoming error: {}", e);
            toolLogUtils.addErrorSystmeLog(publicLog, e.getMessage());
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("PriceController.insertPriceIncoming error: {} ", e);
            toolLogUtils.addErrorSystmeLog(publicLog, e.getMessage());
            result = new RestfulResponse<>(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }
}
